package com.tdd.service.impl;



import com.tdd.mapper.StuMapper;
import com.tdd.pojo.Stu;
import com.tdd.service.StuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {

    public final static Logger logger = LoggerFactory.getLogger(StuServiceImpl.class);
    @Autowired
    public StuMapper stuMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Stu getStuInfo(int id) {
        try{
            logger.info("getStuService开始");
            return stuMapper.selectByPrimaryKey(id);
        }catch (Exception e) {
            return null;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int saveStu(Stu stu) {
        try{
            return stuMapper.insertSelective(stu);
        }catch (Exception e) {
            return 9999;
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateStu(int id) {
        int res = 0;
        try{
            Stu stu = new Stu();
            stu.setId(id);
            stu.setAge(99);
            res = stuMapper.updateByPrimaryKey(stu);
        }catch (Exception e) {
            throw new RuntimeException("查询数据异常！",e);
        }
        return res;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteStu(int id) {
        try{
            return stuMapper.deleteByPrimaryKey(19);
        }catch (Exception e) {
            return 9999;
        }
    }



    public static void main(String args[]) {
        int i = 9;
        int j = 0;
        List<String> list = new ArrayList<>();
        Integer integer = 0;
        String str = "航子";
        list.add("你好");
        add(i,list,integer,str);
        System.out.println(list.toString());
        System.out.println(integer);
        System.out.println(str);
    }

    public static int add(int i, List<String> list, Integer integer, String str){
        if(i == 9) {
            list.add("我是数组");
            integer = 9;
            str = "爽子";
        }

        return 0;
    }
}
